package org.easyframework.redis;

import org.redisson.RedissonRedLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 
 * @ClassName: LockUtil
 * @Description: 红锁工具类
 * @author: daiw
 * @date: 2019年10月15日 下午3:01:31
 */
@Component
public class RedLockUtil {
	@Autowired
	private RedissonClient redissonClient;

	public RedissonRedLock getRedLock(String key) {
		RedissonRedLock redissonRedLock = new RedissonRedLock(getLockInsts(key));
		return redissonRedLock;
	}

	private RLock[] getLockInsts(String key) {
		RLock[] locks = new RLock[5];
		for (int i = 0; i < 5; i++) {
			locks[i] = redissonClient.getLock(key + i);
		}
		return locks;
	}
}
